package no.ntnu.fp.messages.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.MessageCode;
import no.ntnu.fp.model.Appointment;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.model.Meeting;
import no.ntnu.fp.model.Notice;
import no.ntnu.fp.utils.XmlParser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Sent by server to client after successful login.
 * @author caroline
 *
 */

public class LoginSuccessfulMessage implements Message {
	private final List<Appointment> appointmentList;
	private final List<Meeting> meetingList;
	private final List<Notice> noticeList;
	private final Employee employee;
	
	public LoginSuccessfulMessage(){
		this(null, Collections.<Appointment>emptyList(), 
				Collections.<Meeting>emptyList(),
				Collections.<Notice>emptyList());
	}
	
	public LoginSuccessfulMessage(Employee employee, List<Appointment> appointmentList, 
			List<Meeting> meetingList, List<Notice> noticeList){
		this.appointmentList = new ArrayList<Appointment>(appointmentList);
		this.meetingList = new ArrayList<Meeting>(meetingList);
		this.noticeList = new ArrayList<Notice>(noticeList);
		this.employee = employee;
	}
	
	public Employee getEmployee() {
		return employee;
	}
	
	public List<Notice> getNotices() {
		return new ArrayList<Notice>(noticeList);
	}
	
	public List<Appointment> getAppointments() {
		return new ArrayList<Appointment>(appointmentList);
	}
	
	public List<Meeting> getMeetings() {
		return new ArrayList<Meeting>(meetingList);
	}

	@Override
	public MessageCode getMessageCode() {
		return MessageCode.LOGIN_SUCCESSFUL_MESSAGE;
	}

	@Override
	public Message fromXmlString(String xml) {
		Document dom = XmlParser.parseXmlString(xml);
		List<Appointment> appointments = new ArrayList<Appointment>();
		List<Notice> notices = new ArrayList<Notice>();
		List<Meeting> meetings = new ArrayList<Meeting>();
		
		Element appointmentElement 
				= (Element) dom.getElementsByTagName("appointments").item(0);
		NodeList app = dom.getElementsByTagName("appointment");
		for(int i = 0; i < app.getLength(); i++){
			appointments.add(new Appointment((Element)app.item(i)));
		}
	
		Element meetingsElement 
					= (Element) dom.getElementsByTagName("meetings").item(0);
		NodeList meet = meetingsElement.getElementsByTagName("meeting");
		for(int i = 0; i < meet.getLength(); i++){
			meetings.add(new Meeting((Element)meet.item(i)));
		}
		
		NodeList not = dom.getElementsByTagName("notice");
		for(int i = 0; i < not.getLength(); i++){
			notices.add(new Notice((Element)not.item(i)));
		}
		
		return new LoginSuccessfulMessage(new Employee((Element)dom.getElementsByTagName("employee").item(0)),
				appointments, meetings, notices);
	}

	@Override
	public String toString() {
		Document dom = XmlParser.getThreadLocalDocumentBuilder().newDocument();
		Element rootElement = dom.createElement("message");
		rootElement.appendChild(employee.toXml(dom));
		
		Element appointments = dom.createElement("appointments");
		for(Appointment a : this.appointmentList){
			appointments.appendChild(a.toXml(dom));
		}
		rootElement.appendChild(appointments);
		
		Element meetings = dom.createElement("meetings");
		for(Meeting m : this.meetingList){
			meetings.appendChild(m.toXml(dom));
		}
		rootElement.appendChild(meetings);
		
		Element notices = dom.createElement("notices");
		for(Notice n : this.noticeList){
			notices.appendChild(n.toXml(dom));
		}
		rootElement.appendChild(notices);
		
		return getMessageCode() + XmlParser.convertXmlToString(rootElement);
	}
}
